package owg.util.opengl.models;

import java.nio.ShortBuffer;

/**
 * This class is a container for a vertex using signed or unsigned shorts for data. 
 * */
public class VertexS implements VertexShort
{
	public short[] vertex;
	public short[] normal;
	public short[] color;
	public short[] texCoord;
	public short[] texCoord2;
	
	/**Construct a vertex with the given data.
	 * If certain components are known to be unnecessary for the application, those may be null.*/
	public VertexS(short[] v,short[] n,short[] c,short[] t, short[] u)
	{
		vertex  = v;
		normal  = n;
		color   = c;
		texCoord= t;
		texCoord2 = u;
	}
	@Override
	public VertexS clone()
	{
		return new VertexS(vertex==null?null:vertex.clone(),normal==null?null:normal.clone(),
			color==null?null:color.clone(),texCoord==null?null:texCoord.clone(),texCoord2==null?null:texCoord2.clone());
	}
	/**Clone, but don't clone the arrays*/
	public VertexS lazyClone()
	{
		return new VertexS(vertex,normal,color,texCoord,texCoord2);
	}
	
	@Override
	public void put(ShortBuffer vBuf, int numVertexComponents,
		boolean useNormal, byte numColorComponents, byte numTexCoordComponents,
		byte numTexCoordComponents2)
	{
		vBuf.put(vertex, 0, numVertexComponents);
		if (useNormal)
			vBuf.put(normal, 0, 3);
		if (numColorComponents > 0)
		{//Accomodate for missing compontents
			if(color.length < numColorComponents)
			{
				vBuf.put(color, 0, color.length);
				short[] tmp = new short[numColorComponents - color.length];
				tmp[tmp.length-1] = (short) 0xFFFF;//Short colors are scaled so full range is used
				vBuf.put(tmp);
			}
			else
				vBuf.put(color, 0, numColorComponents);
		}
		if (numTexCoordComponents > 0)
			vBuf.put(texCoord, 0, numTexCoordComponents);
		if (numTexCoordComponents2 > 0)
			vBuf.put(texCoord2, 0, numTexCoordComponents2);
	}
}
